import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter/services.dart';
import 'package:paly_android/net/dio_client.dart';
import 'package:paly_android/net/interceptors.dart';
import 'package:paly_android/route/routes.dart';
import 'package:paly_android/ui/login_page.dart';
import 'package:paly_android/ui/main_page.dart';
import 'package:paly_android/util/log_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

initStatusBar() {
  if (Platform.isAndroid) {
    //如果是android设备，状态栏设置为透明沉浸
    SystemUiOverlayStyle _style = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(_style);
  }
}

bool isLogin = false;


void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  Log.init();
  final List<Interceptor> interceptors = <Interceptor>[];
  interceptors.add(LoggingInterceptor());
  configDio(baseUrl: "https://wanandroid.com/", interceptors: interceptors);
  initStatusBar();
  SharedPreferences sp = await SharedPreferences.getInstance();
  String _userId =  sp.getString("userId") ?? "";
  isLogin = _userId.isNotEmpty;

  runApp(MyApp());
}

// void main() {
//   Bloc.observer = SimpleBlocObserver();
//   runApp(App());
// }
class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
        headerBuilder: () => ClassicHeader(),
        // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
        footerBuilder: () => ClassicFooter(),
        // 配置默认底部指示器
        headerTriggerDistance: 80.0,
        // 头部触发刷新的越界距离
        springDescription:
            SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
        // 自定义回弹动画,三个属性值意义请查询flutter api
        maxOverScrollExtent: 100,
        //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
        maxUnderScrollExtent: 0,
        // 底部最大可以拖动的范围
        enableScrollWhenRefreshCompleted: true,
        //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
        enableLoadingWhenFailed: true,
        //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
        hideFooterWhenNotFull: true,
        // Viewport不满一屏时,禁用上拉加载更多功能
        enableBallisticLoad: true,
        // 可以通过惯性滑动触发加载更多
        child: MaterialApp(
          onGenerateRoute: MyRouter.onGenerateRoute,
          initialRoute: isLogin ? MyRouter.MAIN : MyRouter.LOGIN ,
          onGenerateInitialRoutes:(name) {
            LogUtil.e("onGenerateInitialRoutes $name");
            if(MyRouter.MAIN==name){
              return [MaterialPageRoute(builder: (context){
                return MainPage();
              })];
            }

            return [MaterialPageRoute(builder: (context){
              return LoginPage();
            })];
          }

          ,
          // routes: MyRouter.routes,
        ));
  }
}
